<?php

namespace app\admin\controller\manage;

use app\common\controller\Backend;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 巡检记录管理
 *
 * @icon fa fa-circle-o
 */
class Checkrecord extends Backend
{

    /**
     * Checkrecord模型对象
     * @var \app\admin\model\Checkrecord
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Checkrecord;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                    
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $row) {
                $row->visible(['id','date','checkplan','checkplan_detail_start_time','checkplan_detail_end_time','check_user','real_check_user','checkpoint','check_time','check_pics','is_abnormal','abnormal_desc','next_checkpoint','create_time']);
                
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }



    public function recordlist()
    {
//        echo 111;die;
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->where($where)
                ->order('date DESC')
                ->group('check_user_id,date,checkplan_id,checkplan_detail_id')
                ->paginate($limit);

            foreach ($list as $row) {
                $row->visible(['id','date','checkplan','checkplan_id','checkplan_detail_id','checkplan_detail_start_time','checkplan_detail_end_time','check_user_id','check_user']);

            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    public function detial()
    {

        $checkplan_detail_id = $this->request->request('checkplan_detail_id');
        $checkdate = $this->request->request('checkdate');
        $plan_id = $this->request->request('checkplan_id');

        if($checkplan_detail_id){
            $where['checkplan_detail_id'] = $checkplan_detail_id;

        }
        if($checkdate){
            $where['date'] = $checkdate;

        }

        $line = Db::name('checkplan')->field('check_line')->where(array('id'=>$plan_id))->find();
        $pointids=explode(',',rtrim($line['check_line'],','));
        $list=array();
        foreach ($pointids as $pointid) {
            $point_info = Db::name('checkpoint')->field('position_id,checkpoint')->where(array('id'=>$pointid))->find();
            $list[$pointid]['checkpoint']=$point_info['checkpoint'];
            $where['checkpoint_id'] = $pointid;
            $record_info = Db::name('check_record')->field('checkpoint,is_abnormal')->where($where)->find();
            if(!$record_info){
                $list[$pointid]['is_abnormal']='未巡检';
            }else{
                if($record_info['is_abnormal']==1){
                    $list[$pointid]['is_abnormal']='正常';
                }else{
                    $list[$pointid]['is_abnormal']='异常';
                }
            }
        }



//        foreach ($list as $k=>$row) {
//            if($row['is_abnormal']==1){
//                $list[$k]['is_abnormal']='正常';
//            }else{
//                $list[$k]['is_abnormal']='异常';
//            }
//        }
//             echo '<pre />';
//             var_dump($list);
//             echo Db::getLastSql();die;
        $this->view->assign("list", $list);
        return $this->view->fetch();

//        return json(['list' => $list, 'total' => count($list)]);
    }

}
